/**
 * Copyright 2009 Takahiko Ito
 *
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.unigram.oluolu.rqe;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.Set;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;

/**
 * Validate candidate related query pairs by the frequencies. 
 */
public final class RelatedQueryExtractionReducer extends
        Reducer<Text, Text, Text, LongWritable> {
    
    /** minimum frequency of query. */
    private long minimumSupport;

    /**
     * Reduce method.
     *
     * @param key target query
     * @param values candidate query (query may be related to target query)
     * @param context write (key:related query pair, value: frequency)
     * @throws IOException -
     * @throws InterruptedException -
     */
    @Override
    public void reduce(final Text key,
            final Iterable<Text> values,
            final Context context)
            throws IOException, InterruptedException {

        // store the count of each related queries
        Map<Text, Long> candidateQueryMap = new HashMap<Text, Long>();
        for (Text candidateQuery : values) {
            if (candidateQueryMap.containsKey(candidateQuery)) {
                Long count = (Long) candidateQueryMap.get(candidateQuery);
                count += 1;
                candidateQueryMap.put(candidateQuery, count);
            } else {
                candidateQueryMap.put(
                        new Text(candidateQuery.toString()), new Long(1));
            }
        }

        // output only highly related ones
        Set candidateQueries = candidateQueryMap.keySet();
        Iterator iterator = candidateQueries.iterator();
        while (iterator.hasNext()) {
            Text candidateQuery = (Text) iterator.next();
            Long count = (Long) candidateQueryMap.get(candidateQuery);
            if (count >= minimumSupport) {
                context.write(new Text(key.toString()
                        +":"+candidateQuery.toString()), 
                        new LongWritable(count)); 
            }
        }
    }

    /**
     * @param context Context
     */
    @Override
    public void setup(final Context context) {
        Configuration jc = context.getConfiguration();
        this.minimumSupport = RqeConstants.DEFAULT_RQE_MINIMUM_SUPPORT;
        if (context == null || jc == null) {
         /* added default configuration for testing */
            jc = new Configuration();
        }
        this.minimumSupport = jc.getLong(RqeConstants.RQE_MINIMUM_SUPPORT,
                RqeConstants.DEFAULT_RQE_MINIMUM_SUPPORT);
    }
}
